﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Interactivity.InteractionRequest;
using System.Windows;
using System.Windows.Input;
using SPW.Infrastructure.Interfaces;
using CommonServiceLocator;
using SyntacticSugar;
using Prism.Modularity;
using Prism.Regions;
using SPW.Infrastructure.Repositorys;
using SPW.Infrastructure.Utils;
using System.Threading.Tasks;

namespace SPW.GenericDesktop.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        #region 动态属性控制
        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private Visibility _visibility = Visibility.Hidden;
        public Visibility Visibility
        {
            get => _visibility;
            set => SetProperty(ref _visibility, value);
        }
        #endregion

        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager _regionManager;
        private readonly IModuleManager _moduleManager;

        public MainWindowViewModel(IEventAggregator eventAggregator,
            IRegionManager regionManager,
            IModuleManager moduleManager)
        {
            //设置
            EventAggregatorRepository.SetInstance(eventAggregator);
            this._eventAggregator = eventAggregator;
            this._regionManager = regionManager;
            this._moduleManager = moduleManager;

            //界面参数初始化
            Title = Properties.Settings.Default.SoftwareMasterTitle;

            //初始化弹窗
            this.NoBorderPopRequest = new InteractionRequest<INotification>();
            this.GeneralPopRequest = new InteractionRequest<INotification>();

            //订阅事件
            SubscribeEvents();
        }

        #region 初始化方法
        /// <summary>
        /// 订阅事件
        /// </summary>
        private void SubscribeEvents()
        {
            //订阅事件
            this._eventAggregator.GetEvent<Infrastructure.Events.LogInCompletedEvent>().Subscribe(LogInCompleted);
            this._eventAggregator.GetEvent<Infrastructure.Events.EarthRenderEvent>().Subscribe(EarthRenderAction);
        }
        #endregion
        public InteractionRequest<INotification> NoBorderPopRequest { get; set; }
        public InteractionRequest<INotification> GeneralPopRequest { get; set; }

        #region 订阅事件处理
        /// <summary>
        /// 登录完成后续处理
        /// </summary>
        /// <param name="isSuccess">是否成功</param>
        private void LogInCompleted(Infrastructure.Events.LogInCompletedEvent eventArgs)
        {
            if (eventArgs.IsSuccess)
            {
                // 更新属性
                Visibility = Visibility.Visible;
                // 显示主窗口
                var shell = App.Current.MainWindow;
                shell.Show();
                // 加载skyline
                var masterViewInst = ServiceLocator.Current.GetInstance<IMasterView>("EarthViewer");
                this._regionManager.RegisterViewWithRegion("MasterContentRegion", masterViewInst.GetType());
            }
        }

        /// <summary>
        /// 地图渲染完成后续处理
        /// </summary>
        /// <param name="eventArgs"></param>
        public void EarthRenderAction(Infrastructure.Events.EarthRenderEvent eventArgs)
        {
            // 设置窗体样式
            Visibility = Visibility.Visible;
            // 加载模块布局容器窗口
            var layoutViewInst = ServiceLocator.Current.GetInstance<ILayoutView>(Properties.Settings.Default.LayoutView);
            //弹窗登录框
            NoBorderPopRequest.Raise(new Codes.PopupSettings
            {
                Title = Properties.Settings.Default.SoftwareMasterTitle,
                Content = layoutViewInst,
                WindowState = WindowState.Maximized
            });
        }
        #endregion

        #region Commands
        /// <summary>
        /// 主界面加载完毕
        /// </summary>
        public ICommand LoadedCommand
        {
            get => new DelegateCommand(async () =>
            {
                //休眠500 毫秒
                await Task.Delay(500);

                //加载登录界面模块
                var logInViewInst = ServiceLocator.Current.GetInstance<ILogInView>(Properties.Settings.Default.LoginView);
                //弹窗登录框
                NoBorderPopRequest.Raise(new Codes.PopupSettings
                {
                    Title = Properties.Settings.Default.SoftwareMasterTitle,
                    Content = logInViewInst,
                    ShowInTaskbar = true,
                    IsDrag = true
                });
            });
        }
        #endregion
    }
}
